package com.ai.tcc.order.service.Impl;

import com.ai.tcc.order.core.util.lock.RedissLockUtil;
import com.ai.tcc.order.dao.TccOrderMapper;
import com.ai.tcc.order.model.TccOrder;
import com.ai.tcc.order.service.OrderService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * Date:  2018/10/30
 * Time:  13:48
 * Email: siy@hmgsoft.com
 * Created by siy
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService{
    @Resource
    private TccOrderMapper orderMapper;

    @Override
    public TccOrder select(TccOrder order) {
        return orderMapper.selectOne(order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(TccOrder order) {
        TccOrder qs = new TccOrder();
        qs.setNumber(order.getNumber());
        RedissLockUtil.lock("test_1");
        TccOrder rs = orderMapper.selectOne(qs);
        int i = 0;
        if(rs==null){
            i = orderMapper.insertSelective(order);
        }
        RedissLockUtil.unlock("test_1");
        log.info("插入主键=》{} | 插入条数=》{}",order.getId(),i);
        return i;
    }
}
